package com.teach;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Objects;
import java.util.Vector;

public class CollectionsTest {

	public static void main(String[] args) {
		//集合转数组
		//...
		
		//数组转集合
		Collection<String> c2 = new ArrayList();
		String[] as2 = {"D","A","B","C"};
//		c2 = Arrays.asList(as2);//禁止使用
		Collections.addAll(c2, as2);
		System.out.println(c2);
		Collections.addAll(c2, "a","c","d");
		System.out.println(c2);
		System.out.println(as2);
		System.out.println(Arrays.deepToString(as2));
		
		
		//线程安全相关
		c2 = Collections.synchronizedCollection(c2);//线程安全的
		c2 = new Vector<String>();//线程安全的
		c2 = new java.util.concurrent.CopyOnWriteArrayList<String>();
		c2 = new java.util.concurrent.CopyOnWriteArraySet<String>();
		Map<?,?> map = new HashMap();//非线程安全的
		map = Collections.synchronizedMap(map);//线程安全的
		map = new Hashtable();//线程安全的
		map = new java.util.concurrent.ConcurrentHashMap();//线程安全的
		
		//数组拷贝
		//System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
		
		String[] as3 = Arrays.copyOf(as2, 2);
		System.out.println(Arrays.toString(as3));
		
		String[] as4 = Arrays.copyOfRange(as2, 1, 3);
		System.out.println(Arrays.toString(as4));
		
		//排序和搜索
		Arrays.sort(as2);
		System.out.println(Arrays.toString(as2));
		System.out.println(Arrays.binarySearch(as2, "B"));
		
		Arrays.sort(as2,(a,b)->b.compareTo(a));
		System.out.println(Arrays.toString(as2));
		System.out.println(Arrays.binarySearch(as2, "B",(a,b)->b.compareTo(a)));
	
		//jdk1.7+
		String a = null;
		a = "asdf";
		a = Objects.requireNonNull(a, "字符串不能为null");
	
	}
}
